<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="管点名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pipName">
              <a-input v-model="model.pipName" placeholder="请输入管点名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="管点编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pipNo">
              <a-input v-model="model.pipNo" placeholder="请输入管点编号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="posStatus">
              <a-select v-model="model.status">
                <a-select-option :value="1">现状</a-select-option>
                <a-select-option :value="2">规划</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="管点类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pipType">
              <a-select v-model="model.pipType">
                <a-select-option :value="1">雨水井</a-select-option>
                <a-select-option :value="2">污水井</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="管点缺陷" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pipPointDefective">
              <j-dict-select-tag
                type="list"
                v-model="model.pipPointDefective"
                dictCode="ps_point_defective"
                placeholder="请选择管点缺陷"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item
              label="关联点位编号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="associationPosPointNo"
            >
              <a-input v-model="model.associationPosPointNo" placeholder="请输入关联点位编号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="所属区域" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="areaId_dictText">
              <a-input v-model="model.areaId_dictText" disabled placeholder="请输入所属区域"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="道路" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="street">
              <a-input v-model="model.street" placeholder="请输入道路"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="经度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="longitude">
              <a-input-number v-model="model.longitude" placeholder="请输入经度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="纬度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="latitude">
              <a-input-number v-model="model.latitude" placeholder="请输入纬度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="地面高程" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dsm">
              <a-input v-model="model.dsm" placeholder="请输入地面高程"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item
              label="井盖材质"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="manholeCoveMaterial"
            >
              <j-dict-select-tag
                v-model="model.manholeCoveMaterial"
                placeholder="请选择井盖材质"
                dictCode="ps_manholecove_material"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="井盖形状" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="manholeCoveShape">
              <j-dict-select-tag
                v-model="model.manholeCoveShape"
                placeholder="请选择井盖形状"
                dictCode="ps_manholecovet_shape"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="井盖规格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="manholeCoveSize">
              <j-dict-select-tag
                v-model="model.manholeCoveSize"
                placeholder="请选择井盖形状"
                dictCode="ps_manholecover_size"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="井深" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="feature">
              <a-input v-model="model.measureDepth" placeholder="请输入井深"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="井特征" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="feature">
              <a-input v-model="model.feature" placeholder="请输入井特征"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否有防坠网" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="posStatus">
              <a-select v-model="model.isAntiNet">
                <a-select-option value="">请选择</a-select-option>
                <a-select-option :value="1">是</a-select-option>
                <a-select-option :value="0">否</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="关联污水设施" class="ant-form-item-required" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select
                v-model="model.facilityId"
                show-search
                allowClear
                placeholder="请选择"
                :default-active-first-option="false"
                :show-arrow="false"
                :not-found-content="null"
                option-filter-prop="children"
              >
                <!-- 显示终端名称+编号 -->
                <a-select-option :value="undefined">请选择</a-select-option>
                <a-select-option v-for="item in facilityList" :key="item.value" :value="item.value">
                  {{ item.label }}
                </a-select-option>
              </a-select>
              <!--              <a-input v-model="model.terminalId" placeholder="请输入终端编号"></a-input>-->
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
export default {
  name: 'PipPointForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      facilityList:[],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      rowLabelCol: {
        xs: { span: 24 },
        sm: { span: 4 },
      },
      rowWrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
      confirmLoading: false,
      validatorRules: {
        pipName: [{ required: true, message: '请输入管点名称!' }],
        associationPosPointNo: [{ required: true, message: '请输入关联点位编号!' }],
      },
      url: {
        add: '/paishui/pipPoint/add',
        edit: '/paishui/pipPoint/edit',
        queryById: '/paishui/pipPoint/queryById',
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    console.log('备份model原始值', this.modelDefault)
    this.getFacilityList()
  },
  methods: {
    async getFacilityList() {
      try {
      const res = await getAction(`/paishui/paishuiFacility/list/1638530805038878721`, { pageNo: 1, pageSize: 10000 })
        const facilityList= res.result.records||[]
        this.facilityList= facilityList.map((item) => ({
          value:item.id,
          label:item.facilityName,
        }))
      } catch (error) {
        console.log(error)
      }
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'post'
          }
          // let num = Number(this.model.posStatus)
          // console.log('num======',num)
          // this.model.posStatus = num
          // console.log('this.model=======',this.model)
          const { facilityId } = this.model
          const params = { ...this.model, facilityId: facilityId ? facilityId : '' }
          httpAction(httpurl, params, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>
